<div class="modal fade " id="maintainContractModal"  role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="box box-info">
            <div class="box-header with-border">
                <h3 class="box-title">合同维护</h3>
            </div>
            <form class="form-horizontal" role="form" id="maintainContractForm"  method="post"  action="/contractmanager/savecontract" enctype="multipart/form-data">
                <div class="box-body">
                    <div class="form-group">
                        <input id="contractId" name="contractId" type="text" style="display:none;">

                        <input id="productId" name="productId" type="text" style="display:none;">
                        <label class="col-sm-2 control-label" for="productNameLabel">产品名称</label>
                        <div class="col-sm-10">
                            <select id="productName" class=" form-control select2" style="width: 100%"
                                    onChange="contractTypeChange(this.options[this.options.selectedIndex].value,-1)">
                                <option value="-1">请选择</option>
                                <#if productInfoVos??>
                                <#list productInfoVos as vo  >
                                <option value="${vo.productId?if_exists}">${vo.productName?if_exists}</option>
                                </#list>
                                </#if>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <input id="contractType" name="contractType" type="text" style="display:none;">
                        <label class="col-sm-2 control-label" for="contractTypeLabel">合同类型</label>
                        <div class="col-sm-4">
                            <select id="contractTypeList" name="contractTypeList" class=" form-control" style="width: 100%">
                                <option value="-1">请选择</option>
                            </select>
                        </div>
                        <label class="col-sm-2 control-label" for="oaCodeLabel">OA编号</label>
                        <div class="col-sm-4">
                            <input id="oaCode" name="oaCode" type="text" class="form-control" maxlength="100">
                        </div>
                    </div>
                    <div class="form-group">
                        <input id="fileNameText" name="fileNameText" type="text" style="display:none;">
                        <label class="col-sm-2 control-label" for="inputFileLabel">合同文件</label>
                        <div class="col-sm-10">
                            <input type="file" id="file" name="file">
                        </div>
                    </div>
                </div>
                <div class="box-footer">
                    <button type="button" class="btn btn-danger pull-left" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary pull-right" onclick="saveContract()">保存</button>
                </div>
            </form>
        </div>
    </div>
</div>
<#include "../public/js.ftl" >
<#include "../public/css.ftl">
<script src="/js/common.js"></script>
<script>
    $(".select2").select2();

    //初始化页面组件
    function maintainContractInit() {
        //submit时，后端对于disabled="disabled"属性的控件报错，所以区分显示和存值的控件
        $("#contractId").val("");
        $("#contractIdShow").val("");
        $('#productName').val(['-1']).trigger('change');
        $("#productName").removeAttr("disabled");
        $("#contractTypeList").val("-1");
        $("#oaCode").val("");
        $("#file").val("");
        $("#fileNameText").val("");
    }

    //修改时设置数据值
    function modifyContractInit(data) {
        $("#contractIdShow").val(data.contractId);
        $("#contractId").val(data.contractId);
        $('#productName').val([data.productId]).trigger('change');
        $("#productName").attr("disabled","disabled");
        $("#oaCode").val(data.oaCode);
        $("#file").val("");
        contractTypeChange(data.productId, data.contractType);

        $("#fileNameText").val(data.fileName);
    }

    //产品选择时级联处理
    function contractTypeChange(value,contractType) {
        var url = "/contractmanager/querycontracttype?productId="+value;


        //产品列表为"请选择"时，不处理
        if(value == -1) {
            return;
        }

        //根据产品ID请求该类型所对应的合同类型的列表，动态添加到合同类型下拉列表框
        getReq(url, function (list) {
            //清空合同类型列表框
            $("#contractTypeList").empty();
            $("#contractTypeList").append('<option value="-1">' + "请选择" + '</option>');

            if (list.length > 0) {
               for(var i=0; i<list.length; i++){
                   $("#contractTypeList").append('<option value="'+ list[i].keyValue +'">' + list[i].caption + '</option>');
               }

               if(contractType != -1){
                    $("#contractTypeList").val(contractType);
               }
            }
        });
    }

    //保存处理
    function saveContract(){
        //检查产品是否选择
        var productId = $("#productName").val();
        if (productId == "-1" || productId == "") {
            toastr['error']("请选择产品", '异常');
            return;
        }

        //设置productId(@RequestParam绑定单个请求参数值，作用于单个参数，所以需要把productName的值放到productId)
        $("#productId").val(productId);

        //检查合同类型是否选择
        var contractType = $("#contractTypeList").val();
        if (contractType == "-1" || contractType == "") {
            toastr['error']("请选择合同类型", '异常');
            return;
        }

        //合同类型(@RequestParam绑定单个请求参数值，作用于单个参数，所以需要把contractTypeList的值放到contractType)
        $("#contractType").val(contractType);


        var fileNameValue = $("#fileNameText").val();
        //当fileNameValue为空，表示为新增，需检查文件是否为空
        //当fileNameValue不为空，表示为修改，修改时，可不上传文件，只更新信息

        //检查上传文件是否为空
        var filename = $("#file").val();

        if (fileNameValue == "") {
            if (filename == null || filename == undefined || filename == "" || filename.trim() == "") {
                toastr['error']("未选择上传文件", '异常');
                return;
            }
        }

        if (filename.length > 100) {
            toastr['error']("文件名不能超过100个字符","异常");
            return;
        }

        //OA编号
        var oaCode = $("#oaCode").val();
        var length = strlen(oaCode);
        if (length > 100) {
            toastr['error']("OA编号不能超过100个字符","异常");
            return;
        }

        var contractId = $("#contractId").val();

        //上传文件
		var data = {
		    contractId : contractId,
            productId : productId,
            contractType : contractType,
            oaCode : oaCode
		};

		fileReq("contractmanager/savecontract","file",data,function(result){
			if(result.success) {
				toastr['success']('文件上传成功', '提示');
				maintainContractInit();
				$("#contractTable").bootstrapTable("refresh");
			} else {
				toastr['error'](result.msg, '提示');
			}
		});
    }
</script>
